/****************************************************************************
 * Copyright (C) 2012
 * by Dj_Skual
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any
 * damages arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any
 * purpose, including commercial applications, and to alter it and
 * redistribute it freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you
 * must not claim that you wrote the original software. If you use
 * this software in a product, an acknowledgment in the product
 * documentation would be appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and
 * must not be misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source
 * distribution.
 *
 * ScreenSaver.cpp
 *
 * for SaveGame Manager GX 2012
 ***************************************************************************/
#include "ScreenSaver.hpp"
#include "../Controls/Application.h"
#include "../Themes/CTheme.h"
#include "../System/input.h"

ScreenSaver::ScreenSaver()
	: GuiFrame(screenwidth, screenheight)
	, sr(false)
	, needClose(false)
	, exitRequested(false)
{
	BackgroundImg = new GuiImage(screenwidth, screenheight, thColor("r=0 g=0 b=0 a=255 - screensaver background color"));
	
	GXlogo = Resources::GetImageData("logo_SGMGX.png");
	GXlogoImg = new GuiImage(GXlogo);
	GXlogoImg->SetPosition(172, 152);
	
	Append(BackgroundImg);
	Append(GXlogoImg);
	
	UpdateTimer = new Timer();

	SetEffect(EFFECT_FADE, 20);
	Application::Instance()->SetUpdateOnly(this);
	Application::Instance()->Append(this);
}

ScreenSaver::~ScreenSaver()
{
	RemoveAll();

	delete GXlogoImg;
	delete BackgroundImg;
	
	Resources::Remove(GXlogo);
	
	delete UpdateTimer;
}

void ScreenSaver::quit()
{
	SetEffect(EFFECT_FADE, -20);
	Close();
}

bool ScreenSaver::NeedClose()
{
	bool b = needClose;
	needClose = false;
	return b;
}

void ScreenSaver::Draw()
{
	if(!exitRequested)
	{
		if(!ControlActivityTimeout())
		{
			needClose = true;
			exitRequested = true;
		}
		else
		{
			if (!sr)
			{
				srand(time(0));
				sr=true;
			}
			
			if(UpdateTimer->elapsed() > 4)
			{
				UpdateTimer->reset();
				GXlogoImg->SetPosition((rand() % 345), (rand() % 305));
			}
		}
	}
	
	GuiFrame::Draw();
}
